package com.ftg.youx.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * 消息转换类
 *
 * @author 布衣
 */
@Configuration
public class SpringMessageConverts implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 创建自定义的 ObjectMapper 实例
        ObjectMapper objectMapper = new ObjectMapper();
        // 注册 Java 8 时间模块，以便更好地处理日期和时间
        objectMapper.registerModule(new JavaTimeModule());
        // 设置序列化和反序列化的特性
        objectMapper.findAndRegisterModules();
        objectMapper.configure(com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

        // 创建自定义的 MappingJackson2HttpMessageConverter 实例
        MappingJackson2HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter();
        jacksonConverter.setObjectMapper(objectMapper);

        // 将自定义的转换器添加到 converters 列表中
        converters.add(jacksonConverter);
    }
}